#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QLabel, QLineEdit, 
                           QPushButton, QComboBox, QCompleter)
from PyQt5.QtCore import Qt, pyqtSignal

class FilterWidget(QWidget):
    """
    过滤器窗口组件，用于设置数据包过滤条件
    """
    
    # 自定义信号
    filter_applied = pyqtSignal(str)  # 发送过滤器字符串
    
    def __init__(self, parent=None):
        """
        初始化过滤器窗口组件
        """
        super().__init__(parent)
        self._setup_ui()
        
    def _setup_ui(self):
        """
        设置UI组件
        """
        # 创建水平布局
        layout = QHBoxLayout(self)
        layout.setContentsMargins(5, 5, 5, 5)
        
        # 添加过滤器标签
        self.filter_label = QLabel("显示过滤器:")
        layout.addWidget(self.filter_label)
        
        # 添加过滤器快速选择下拉框
        self.filter_combo = QComboBox()
        self.filter_combo.setEditable(False)
        self.filter_combo.addItem("无过滤器")
        self.filter_combo.addItem("TCP协议")
        self.filter_combo.addItem("UDP协议")
        self.filter_combo.addItem("HTTP协议")
        self.filter_combo.addItem("DNS协议")
        self.filter_combo.addItem("ICMP协议")
        self.filter_combo.setMinimumWidth(120)
        self.filter_combo.currentIndexChanged.connect(self._on_combo_changed)
        layout.addWidget(self.filter_combo)
        
        # 添加过滤器输入框
        self.filter_edit = QLineEdit()
        self.filter_edit.setPlaceholderText("输入过滤器表达式...")
        
        # 添加自动完成
        self._setup_completer()
        
        # 连接回车键信号
        self.filter_edit.returnPressed.connect(self._on_filter_applied)
        
        layout.addWidget(self.filter_edit, 1)  # 输入框占据更多空间
        
        # 添加应用按钮
        self.apply_button = QPushButton("应用")
        self.apply_button.clicked.connect(self._on_filter_applied)
        layout.addWidget(self.apply_button)
        
        # 添加清除按钮
        self.clear_button = QPushButton("清除")
        self.clear_button.clicked.connect(self._on_clear_filter)
        layout.addWidget(self.clear_button)
        
        self.setLayout(layout)
        
    def _setup_completer(self):
        """
        设置输入框自动完成功能
        """
        # 常用过滤器关键字
        filter_keywords = [
            "tcp", "udp", "icmp", "http", "dns", "arp", "ip", "ipv6",
            "src", "dst", "port", "host", "net", "gateway",
            "tcp.port", "udp.port", "ip.addr", "ip.src", "ip.dst",
            "http.request", "http.response", "dns.query", "dns.resp"
        ]
        
        # 创建自动完成器
        completer = QCompleter(filter_keywords, self)
        completer.setCaseSensitivity(Qt.CaseInsensitive)
        self.filter_edit.setCompleter(completer)
        
    def _on_combo_changed(self, index):
        """
        处理快速选择下拉框变化
        
        Args:
            index: 选中的索引
        """
        # 根据选择设置过滤器
        if index == 0:  # 无过滤器
            self.filter_edit.clear()
        elif index == 1:  # TCP协议
            self.filter_edit.setText("tcp")
        elif index == 2:  # UDP协议
            self.filter_edit.setText("udp")
        elif index == 3:  # HTTP协议
            self.filter_edit.setText("http")
        elif index == 4:  # DNS协议
            self.filter_edit.setText("dns or udp port 53")
        elif index == 5:  # ICMP协议
            self.filter_edit.setText("icmp")
            
    def _on_filter_applied(self):
        """
        处理过滤器应用
        """
        filter_text = self.filter_edit.text().strip()
        self.filter_applied.emit(filter_text)
        
    def _on_clear_filter(self):
        """
        清除过滤器
        """
        self.filter_edit.clear()
        self.filter_combo.setCurrentIndex(0)
        self.filter_applied.emit("")
        
    def get_filter(self):
        """
        获取当前过滤器文本
        
        Returns:
            str: 过滤器文本
        """
        return self.filter_edit.text().strip()
        
    def set_filter(self, filter_text):
        """
        设置过滤器文本
        
        Args:
            filter_text: 过滤器文本
        """
        self.filter_edit.setText(filter_text)
        
        # 尝试匹配下拉框
        if not filter_text:
            self.filter_combo.setCurrentIndex(0)
        elif filter_text == "tcp":
            self.filter_combo.setCurrentIndex(1)
        elif filter_text == "udp":
            self.filter_combo.setCurrentIndex(2)
        elif filter_text == "http":
            self.filter_combo.setCurrentIndex(3)
        elif filter_text == "dns or udp port 53":
            self.filter_combo.setCurrentIndex(4)
        elif filter_text == "icmp":
            self.filter_combo.setCurrentIndex(5) 